package de.baumann.browser.activitys

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.fragments.WebSiteFragment
import de.baumann.browser.iview.IBaseView
import de.baumann.browser.utils.ViewUtil
import de.baumann.browser.views.tab.Tab
import de.baumann.browser.views.tab.TabManager
import de.baumann.browser.views.stackview.UCTabCard
import de.baumann.browser.web.WindowUiController
import kotlinx.android.synthetic.main.activity_web_site.*
import kotlinx.android.synthetic.main.layout_uc_pagers_manager.*

class WebSiteActivity : BaseOdinActivity<IBaseView, BasePresenter<IBaseView>>(), WindowUiController {

    companion object {
        fun startWebSiteActivity(context: Context, content: String) {
            context.startActivity(Intent(context, WebSiteActivity::class.java).apply {
                putExtra("content", content)
            })
        }
    }

    private var content = ""
    private lateinit var fragment: WebSiteFragment

    override fun getLayout(): Int {
        return R.layout.activity_web_site
    }

    private lateinit var mTabManager: TabManager

    override fun initView() {
        content = intent.getStringExtra("content")
        initTab()
    }

    fun initTab() {
        mTabManager = TabManager(mContext, this)
        if (mTabManager.tabCount <= 0) {
            addTab(false)
        }

        ucStackView.setAdapter(mTabManager.tabAdapter)
        ucStackView.setOnChildDismissedListener {
            mTabManager.removeTab(it)
            if (ucStackView.childCount <= 0) {
                addTab(true)
            }
        }

        ivAddPager.setOnClickListener {
            addTab(true)
        }

        tvBack.setOnClickListener {
            hideTabManager(true)
        }
    }

    private fun addTab(animate: Boolean) {
        if (animate) {
            mContentWrapper.bringToFront()
            animateShowFromBottomToTop(mContentWrapper, Runnable {
                // 把页面管理页隐藏
                hideTabManager(false)
            })
        }
        mTabManager.createNewTab()
    }

    private fun hideTabManager(animated: Boolean) {
        if (animated) {
            ucStackView.animateShow(mTabManager.currentPosition,
                    mContentWrapper,
                    flPagersManager, false) {
                flPagersManager.visibility = View.GONE
            }
            val selectedChild: View = ucStackView.selectedChild
            val card: UCTabCard = selectedChild.findViewById(R.id.ucTabCard)
            card.active(false, 350, 40, null)
            ViewUtil.animateShowFromAlpha(flPagersManager.findViewById(R.id.bottomBar),
                    false, animated, 350, 40, null)
        }
        mContentWrapper.visibility = View.VISIBLE
        flPagersManager.visibility = View.GONE
    }


    private fun animateShowFromBottomToTop(view: View, onCompleteRunnable: Runnable) {
        view.visibility = View.VISIBLE
        val animator = ObjectAnimator.ofFloat(
                view,
                "translationY",
                ViewUtil.getScreenSize(mContext).y.toFloat(), 0f)
        animator.duration = 500
        animator.start()
        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                super.onAnimationEnd(animation)
                onCompleteRunnable.run()
            }
        })
    }

    override fun initData() {
    }


    fun onWindowsClick() {
        mTabManager.currentTab.tabUrl = fragment.url
        mTabManager.currentTab.tabTitle = fragment.title
        mTabManager.capture2()
        flPagersManager.bringToFront()
        flPagersManager.visibility = View.VISIBLE
        ucStackView.animateShow(mTabManager.currentPosition, mContentWrapper, flPagersManager, true) {
            mContentWrapper.visibility = View.GONE
        }
        val selChild = ucStackView.selectedChild
        if (selChild != null) {
            val card = selChild.findViewById<UCTabCard>(R.id.ucTabCard)
            card.active(true, 350, 40, null)
        }

        ViewUtil.animateShowFromAlpha(flPagersManager.findViewById(R.id.bottomBar),
                true, true, 300, 40, null)
    }


    override fun createPresenter(): BasePresenter<IBaseView>? {
        return null
    }

    override fun createView(): IBaseView? {
        return null
    }

    override fun onCloseTab(tab: Tab?) {
        ucStackView.closeTab(mTabManager.getTabPosition(tab))
    }

    private var tabCount = 1
    override fun onTabCountChanged(tabCount: Int) {
        this.tabCount = tabCount
    }

    fun getTabCount(): Int {
        return tabCount
    }

    override fun getContext(): Context {
        return mContext
    }

    override fun onCreateTab(tab: Tab) {
        fragment = WebSiteFragment.getWebSiteFragment(content)
        supportFragmentManager.beginTransaction()
                .add(R.id.mContentWrapper, fragment)
                .commit()
        content = ""
    }

    override fun onSelectTab(tab: Tab) {
        fragment = WebSiteFragment.getWebSiteFragment(tab.tabUrl)
        supportFragmentManager.beginTransaction()
                .replace(R.id.mContentWrapper, fragment)
                .commit()

        mTabManager.setActiveTab(tab)
        val index = mTabManager.getTabPosition(tab)
        ucStackView.selectTab(index) {
            mContentWrapper.visibility = View.VISIBLE
            flPagersManager.visibility = View.GONE
        }
    }

    override fun showBindEth(): Boolean {
        return false
    }
}
